package org.elepower.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.elepower.dao.FunctionDao;
import org.elepower.dao.RoleDao;
import org.elepower.pojo.PsRole;
import org.elepower.pojo.PsUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.awt.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleDao roleDao;
    @Autowired
    private FunctionDao functionDao;


    @RequestMapping("/selectRole")
    @ResponseBody
    public Map<String,Object> selectRole(@RequestParam(defaultValue = "1" )Integer page , @RequestParam(defaultValue = "10") Integer limit, PsRole psRole) throws Exception{
        Map<String,Object> map = new HashMap<>();
        Page<PsRole> pager = PageHelper.startPage(page,limit);

        List<PsRole> roleList = roleDao.getlist(psRole);

        map.put("count",pager.getTotal());
        map.put("data",roleList);
        map.put("code",0);
        map.put("msg","");
        return map;
    }

    @RequestMapping("/selectRoleAll")
    @ResponseBody
    public List<PsRole> selectRoleAll() throws Exception{
        return roleDao.getAll();
    }

    @RequestMapping("/index")
    public String index() throws Exception{
        return "system/role/index";
    }

    /**
     * 查询制定角色所拥有的权限编号
     * @param roleId
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectFunIds")
    @ResponseBody
    public List<Integer> selectFunIds(Integer roleId) throws Exception{
        return roleDao.getFunIdByRoleId(roleId);
    }

    @RequestMapping("/form")
    public String addform() throws Exception{
        return "system/role/form";
    }

    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> add(PsRole psRole, HttpSession session) throws Exception{
        PsUser psUser = (PsUser)session.getAttribute(Constants.LOGIN_USER);
        Map<String,Object> map = new HashMap<>();
        if(psRole.getIsStart() == null){
            psRole.setIsStart(1);
        }
        psRole.setCreatedBy(psUser.getUsername());
        roleDao.add(psRole);

        map.put("result",true);
        return map;
    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public Map<String,Object> batchDelete(Integer id) throws Exception{
        Map<String,Object> map = new HashMap<>();
        System.out.println(id);
        //删除用户角色
        roleDao.deleteUserRoleByRoleId(id);
        //删除角色权限
        functionDao.delete(id);
        //删除角色
        roleDao.delete(id);
        map.put("result",true);
        return map;
    }

    @RequestMapping("/getRole")
    @ResponseBody
    public PsRole getRole(Integer id) throws Exception{
        return roleDao.get(id);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> update(PsRole psRole) throws Exception{
        Map<String,Object> map = new HashMap<>();
        psRole.setLastUpdateTime(new Date());
        roleDao.update(psRole);
        map.put("result",true);
        return map;
    }

    @RequestMapping("/addRoleFunction")
    @ResponseBody
    public Map<String,Object> addRoleFunction(Integer roleId,Integer[] functionIds) throws Exception{
        Map<String,Object> map = new HashMap<>();
        //删除角色的所有权限
        roleDao.deleteRoleFunction(roleId);
        //添加角色权限
        roleDao.addRoleFunction(roleId,functionIds);
        map.put("result",true);
        return map;
    }



    @RequestMapping("/selectUserRole")
    @ResponseBody
    public int selectUserRole(Integer roleId) throws Exception{
        return roleDao.getUserRoleByRoleId(roleId);
    }
}
